// __PUBLIC__/js/page/admin/copydb/doimportdata
require(['../../common'], function() {
  require(['copydb.doimportdata'])
})

define('copydb.doimportdata', [
    'jquery',
    'page/grid'
  ], function($, Grid) {
    'use strict';
    var $table = $('.table')
    var mygrid = new Grid($table)
    mygrid.init()

    function getSelected() {
      var selected = mygrid.getSelected()
      if (!selected.count) {
        alert(lang.pleaseSelectAData)
        return
      }
      return selected
    }

    function getASelected(t) {
      var current = mygrid.getCurrent(t)
      var selected = {
        id: [current.id],
        index: [current.index]
      }
      console.log(selected)
      return selected
    }

    $(document)
      .on('click', '.A_import', function(e) {
        e.preventDefault()
        importData(getASelected(this))
      })
       .on('click', '.A_importnew', function(e) {
        e.preventDefault()
        importnewData(getASelected(this))
      })
      .on('click', '#J_import', function(e) {
        e.preventDefault()
        var selected = getSelected()
        importData(selected)
      })

    function importData(selected) {
      $.post(APP + '?app=admin&m=CopyDB&a=beginImportData', {
        ids: selected.id.join()
      })
        .done(function(res) {
          if (res == '0') {
            alert(lang.fails)
          } else {
            /*$('#sysdeleteuser').html(res)*/
            alert(lang.success)
          }
        })
    }
    function importnewData(selected) {
        $.post(APP + '?app=admin&m=CopyDB&a=beginImportnewData', {
          ids: selected.id.join()
        })
          .done(function(res) {
            if (res == '0') {
              alert(lang.fails)
            } else {
              /*$('#sysdeleteuser').html(res)*/
              alert(lang.success)
            }
          })
      }


  })
